home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / ds3100.md / gdb / RCS / mips-tdep.c,v < prev    next >
Encoding:
Text File  |  1992-07-01  |  25.3 KB  |  846 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.17.11.48.43;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Target-dependent code for the MIPS architecture, for GDB, the GNU Debugger.
  26.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  27.    Contributed by Alessandro Forin(af@@cs.cmu.edu) at CMU
  28.    and by Per Bothner(bothner@@cs.wisc.edu) at U.Wisconsin.
  29.  
  30. This file is part of GDB.
  31.  
  32. This program is free software; you can redistribute it and/or modify
  33. it under the terms of the GNU General Public License as published by
  34. the Free Software Foundation; either version 2 of the License, or
  35. (at your option) any later version.
  36.  
  37. This program is distributed in the hope that it will be useful,
  38. but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  40. GNU General Public License for more details.
  41.  
  42. You should have received a copy of the GNU General Public License
  43. along with this program; if not, write to the Free Software
  44. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  45.  
  46. #include "defs.h"
  47. #include "frame.h"
  48. #include "inferior.h"
  49. #include "symtab.h"
  50. #include "value.h"
  51. #include "gdbcmd.h"
  52. #include "language.h"
  53.  
  54. #ifdef USG
  55. #include <sys/types.h>
  56. #endif
  57.  
  58. #include <sys/param.h>
  59. #include <sys/dir.h>
  60. #include <signal.h>
  61. #include <sys/ioctl.h>
  62.  
  63. #include <setjmp.h>
  64.  
  65. #include "gdbcore.h"
  66. #include "symfile.h"
  67. #include "objfiles.h"
  68.  
  69. #ifndef    MIPSMAGIC
  70. #ifdef MIPSEL
  71. #define MIPSMAGIC    MIPSELMAGIC
  72. #else
  73. #define MIPSMAGIC    MIPSEBMAGIC
  74. #endif
  75. #endif
  76.  
  77. #define VM_MIN_ADDRESS (unsigned)0x400000
  78.  
  79. #include <sys/user.h>        /* After a.out.h  */
  80. #include <sys/file.h>
  81. #include <sys/stat.h>
  82.  
  83.  
  84. #define PROC_LOW_ADDR(proc) ((proc)->pdr.adr) /* least address */
  85. #define PROC_HIGH_ADDR(proc) ((proc)->pdr.iline) /* upper address bound */
  86. #define PROC_FRAME_OFFSET(proc) ((proc)->pdr.frameoffset)
  87. #define PROC_FRAME_REG(proc) ((proc)->pdr.framereg)
  88. #define PROC_REG_MASK(proc) ((proc)->pdr.regmask)
  89. #define PROC_FREG_MASK(proc) ((proc)->pdr.fregmask)
  90. #define PROC_REG_OFFSET(proc) ((proc)->pdr.regoffset)
  91. #define PROC_FREG_OFFSET(proc) ((proc)->pdr.fregoffset)
  92. #define PROC_PC_REG(proc) ((proc)->pdr.pcreg)
  93. #define PROC_SYMBOL(proc) (*(struct symbol**)&(proc)->pdr.isym)
  94. #define _PROC_MAGIC_ 0x0F0F0F0F
  95. #define PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym == _PROC_MAGIC_)
  96. #define SET_PROC_DESC_IS_DUMMY(proc) ((proc)->pdr.isym = _PROC_MAGIC_)
  97.  
  98. struct linked_proc_info
  99. {
  100.   struct mips_extra_func_info info;
  101.   struct linked_proc_info *next;
  102. } * linked_proc_desc_table = NULL;
  103.  
  104.  
  105. #define READ_FRAME_REG(fi, regno) read_next_frame_reg((fi)->next, regno)
  106.  
  107. static int
  108. read_next_frame_reg(fi, regno)
  109.      FRAME fi;
  110.      int regno;
  111. {
  112. #define SIGFRAME_BASE   sizeof(struct sigcontext)
  113. #define SIGFRAME_PC_OFF (-SIGFRAME_BASE+ 2*sizeof(int))
  114. #define SIGFRAME_SP_OFF (-SIGFRAME_BASE+32*sizeof(int))
  115. #define SIGFRAME_RA_OFF (-SIGFRAME_BASE+34*sizeof(int))
  116.   for (; fi; fi = fi->next)
  117.       if (in_sigtramp(fi->pc, 0)) {
  118.       /* No idea if this code works. --PB. */
  119.       int offset;
  120.       if (regno == PC_REGNUM) offset = SIGFRAME_PC_OFF;
  121.       else if (regno == RA_REGNUM) offset = SIGFRAME_RA_OFF;
  122.       else if (regno == SP_REGNUM) offset = SIGFRAME_SP_OFF;
  123.       else return 0;
  124.       return read_memory_integer(fi->frame + offset, 4);
  125.       }
  126.       else if (regno == SP_REGNUM) return fi->frame;
  127.       else if (fi->saved_regs->regs[regno])
  128.     return read_memory_integer(fi->saved_regs->regs[regno], 4);
  129.   return read_register(regno);
  130. }
  131.  
  132. int
  133. mips_frame_saved_pc(frame)
  134.      FRAME frame;
  135. {
  136.   mips_extra_func_info_t proc_desc = frame->proc_desc;
  137.   int pcreg = proc_desc ? PROC_PC_REG(proc_desc) : RA_REGNUM;
  138.  
  139.   if (proc_desc && PROC_DESC_IS_DUMMY(proc_desc))
  140.       return read_memory_integer(frame->frame - 4, 4);
  141.  
  142.   return read_next_frame_reg(frame, pcreg);
  143. }
  144.  
  145. static struct mips_extra_func_info temp_proc_desc;
  146. static struct frame_saved_regs temp_saved_regs;
  147.  
  148. static CORE_ADDR
  149. heuristic_proc_start(pc)
  150.     CORE_ADDR pc;
  151. {
  152.  
  153.     CORE_ADDR start_pc = pc;
  154.     CORE_ADDR fence = start_pc - 200;
  155.     if (fence < VM_MIN_ADDRESS) fence = VM_MIN_ADDRESS;
  156.     /* search back for previous return */
  157.     for (start_pc -= 4; ; start_pc -= 4)
  158.     if (start_pc < fence) return 0; 
  159.     else if (ABOUT_TO_RETURN(start_pc))
  160.         break;
  161.  
  162.     start_pc += 8; /* skip return, and its delay slot */
  163. #if 0
  164.     /* skip nops (usually 1) 0 - is this */
  165.     while (start_pc < pc && read_memory_integer (start_pc, 4) == 0)
  166.     start_pc += 4;
  167. #endif
  168.     return start_pc;
  169. }
  170.  
  171. static mips_extra_func_info_t
  172. heuristic_proc_desc(start_pc, limit_pc, next_frame)
  173.     CORE_ADDR start_pc, limit_pc;
  174.     FRAME next_frame;
  175. {
  176.     CORE_ADDR sp = next_frame ? next_frame->frame : read_register (SP_REGNUM);
  177.     CORE_ADDR cur_pc;
  178.     int frame_size;
  179.     int has_frame_reg = 0;
  180.     int reg30; /* Value of $r30. Used by gcc for frame-pointer */
  181.     unsigned long reg_mask = 0;
  182.  
  183.     if (start_pc == 0) return NULL;
  184.     bzero(&temp_proc_desc, sizeof(temp_proc_desc));
  185.     bzero(&temp_saved_regs, sizeof(struct frame_saved_regs));
  186.     if (start_pc + 200 < limit_pc) limit_pc = start_pc + 200;
  187.   restart:
  188.     frame_size = 0;
  189.     for (cur_pc = start_pc; cur_pc < limit_pc; cur_pc += 4) {
  190.     unsigned long word;
  191.     int status;
  192.  
  193.     status = read_memory_nobpt (cur_pc, &word, 4); 
  194.     if (status) memory_error (status, cur_pc); 
  195.     SWAP_TARGET_AND_HOST (&word, sizeof (word));
  196.     if ((word & 0xFFFF0000) == 0x27bd0000) /* addiu $sp,$sp,-i */
  197.         frame_size += (-word) & 0xFFFF;
  198.     else if ((word & 0xFFFF0000) == 0x23bd0000) /* addu $sp,$sp,-i */
  199.         frame_size += (-word) & 0xFFFF;
  200.     else if ((word & 0xFFE00000) == 0xafa00000) { /* sw reg,offset($sp) */
  201.         int reg = (word & 0x001F0000) >> 16;
  202.         reg_mask |= 1 << reg;
  203.         temp_saved_regs.regs[reg] = sp + (short)word;
  204.     }
  205.     else if ((word & 0xFFFF0000) == 0x27be0000) { /* addiu $30,$sp,size */
  206.         if ((unsigned short)word != frame_size)
  207.         reg30 = sp + (unsigned short)word;
  208.         else if (!has_frame_reg) {
  209.         int alloca_adjust;
  210.         has_frame_reg = 1;
  211.         reg30 = read_next_frame_reg(next_frame, 30);
  212.         alloca_adjust = reg30 - (sp + (unsigned short)word);
  213.         if (alloca_adjust > 0) {
  214.             /* FP > SP + frame_size. This may be because
  215.             /* of an alloca or somethings similar.
  216.              * Fix sp to "pre-alloca" value, and try again.
  217.              */
  218.             sp += alloca_adjust;
  219.             goto restart;
  220.         }
  221.         }
  222.     }
  223.     else if ((word & 0xFFE00000) == 0xafc00000) { /* sw reg,offset($30) */
  224.         int reg = (word & 0x001F0000) >> 16;
  225.         reg_mask |= 1 << reg;
  226.         temp_saved_regs.regs[reg] = reg30 + (short)word;
  227.     }
  228.     }
  229.     if (has_frame_reg) {
  230.     PROC_FRAME_REG(&temp_proc_desc) = 30;
  231.     PROC_FRAME_OFFSET(&temp_proc_desc) = 0;
  232.     }
  233.     else {
  234.     PROC_FRAME_REG(&temp_proc_desc) = SP_REGNUM;
  235.     PROC_FRAME_OFFSET(&temp_proc_desc) = frame_size;
  236.     }
  237.     PROC_REG_MASK(&temp_proc_desc) = reg_mask;
  238.     PROC_PC_REG(&temp_proc_desc) = RA_REGNUM;
  239.     return &temp_proc_desc;
  240. }
  241.  
  242. static mips_extra_func_info_t
  243. find_proc_desc(pc, next_frame)
  244.     CORE_ADDR pc;
  245.     FRAME next_frame;
  246. {
  247.   mips_extra_func_info_t proc_desc;
  248.   struct block *b = block_for_pc(pc);
  249.   struct symbol *sym =
  250.       b ? lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL) : NULL;
  251.  
  252.   if (sym)
  253.     {
  254.     /* IF this is the topmost frame AND
  255.      * (this proc does not have debugging information OR
  256.      * the PC is in the procedure prologue)
  257.      * THEN create a "heuristic" proc_desc (by analyzing
  258.      * the actual code) to replace the "official" proc_desc.
  259.      */
  260.     proc_desc = (mips_extra_func_info_t)SYMBOL_VALUE(sym);
  261.     if (next_frame == NULL) {
  262.         struct symtab_and_line val;
  263.         struct symbol *proc_symbol =
  264.         PROC_DESC_IS_DUMMY(proc_desc) ? 0 : PROC_SYMBOL(proc_desc);
  265.  
  266.         if (proc_symbol) {
  267.         val = find_pc_line (BLOCK_START
  268.                     (SYMBOL_BLOCK_VALUE(proc_symbol)),
  269.                     0);
  270.         val.pc = val.end ? val.end : pc;
  271.         }
  272.         if (!proc_symbol || pc < val.pc) {
  273.         mips_extra_func_info_t found_heuristic =
  274.             heuristic_proc_desc(PROC_LOW_ADDR(proc_desc),
  275.                     pc, next_frame);
  276.         if (found_heuristic) proc_desc = found_heuristic;
  277.         }
  278.     }
  279.     }
  280.   else
  281.     {
  282.       /* Is linked_proc_desc_table really necessary?  It only seems to be used
  283.      by procedure call dummys.  However, the procedures being called ought
  284.      to have their own proc_descs, and even if they don't,
  285.      heuristic_proc_desc knows how to create them! */
  286.  
  287.       register struct linked_proc_info *link;
  288.       for (link = linked_proc_desc_table; link; link = link->next)
  289.       if (PROC_LOW_ADDR(&link->info) <= pc
  290.           && PROC_HIGH_ADDR(&link->info) > pc)
  291.           return &link->info;
  292.       proc_desc =
  293.       heuristic_proc_desc(heuristic_proc_start(pc), pc, next_frame);
  294.     }
  295.   return proc_desc;
  296. }
  297.  
  298. mips_extra_func_info_t cached_proc_desc;
  299.  
  300. FRAME_ADDR
  301. mips_frame_chain(frame)
  302.     FRAME frame;
  303. {
  304.     mips_extra_func_info_t proc_desc;
  305.     CORE_ADDR saved_pc = FRAME_SAVED_PC(frame);
  306.  
  307.     if (saved_pc == 0 || inside_entry_file (saved_pc))
  308.       return 0;
  309.  
  310.     proc_desc = find_proc_desc(saved_pc, frame);
  311.     if (!proc_desc)
  312.       return 0;
  313.  
  314.     cached_proc_desc = proc_desc;
  315.     return read_next_frame_reg(frame, PROC_FRAME_REG(proc_desc))
  316.       + PROC_FRAME_OFFSET(proc_desc);
  317. }
  318.  
  319. void
  320. init_extra_frame_info(fci)
  321.      struct frame_info *fci;
  322. {
  323.   extern struct obstack frame_cache_obstack;
  324.   /* Use proc_desc calculated in frame_chain */
  325.   mips_extra_func_info_t proc_desc = fci->next ? cached_proc_desc :
  326.       find_proc_desc(fci->pc, fci->next);
  327.  
  328.   fci->saved_regs = (struct frame_saved_regs*)
  329.     obstack_alloc (&frame_cache_obstack, sizeof(struct frame_saved_regs));
  330.   bzero(fci->saved_regs, sizeof(struct frame_saved_regs));
  331.   fci->proc_desc =
  332.       proc_desc == &temp_proc_desc ? 0 : proc_desc;
  333.   if (proc_desc)
  334.     {
  335.       int ireg;
  336.       CORE_ADDR reg_position;
  337.       unsigned long mask;
  338.       /* r0 bit means kernel trap */
  339.       int kernel_trap = PROC_REG_MASK(proc_desc) & 1;
  340.  
  341.       /* Fixup frame-pointer - only needed for top frame */
  342.       /* This may not be quite right, if procedure has a real frame register */
  343.       if (fci->pc == PROC_LOW_ADDR(proc_desc))
  344.       fci->frame = read_register (SP_REGNUM);
  345.       else
  346.       fci->frame = READ_FRAME_REG(fci, PROC_FRAME_REG(proc_desc))
  347.           + PROC_FRAME_OFFSET(proc_desc);
  348.  
  349.       if (proc_desc == &temp_proc_desc)
  350.       *fci->saved_regs = temp_saved_regs;
  351.       else
  352.       {
  353.       /* find which general-purpose registers were saved */
  354.       reg_position = fci->frame + PROC_REG_OFFSET(proc_desc);
  355.       mask = kernel_trap ? 0xFFFFFFFF : PROC_REG_MASK(proc_desc);
  356.       for (ireg= 31; mask; --ireg, mask <<= 1)
  357.           if (mask & 0x80000000)
  358.           {
  359.           fci->saved_regs->regs[ireg] = reg_position;
  360.           reg_position -= 4;
  361.           }
  362.       /* find which floating-point registers were saved */
  363.       reg_position = fci->frame + PROC_FREG_OFFSET(proc_desc);
  364.       /* The freg_offset points to where the first *double* register is saved.
  365.        * So skip to the high-order word. */
  366.       reg_position += 4;
  367.       mask = kernel_trap ? 0xFFFFFFFF : PROC_FREG_MASK(proc_desc);
  368.       for (ireg = 31; mask; --ireg, mask <<= 1)
  369.           if (mask & 0x80000000)
  370.           {
  371.           fci->saved_regs->regs[FP0_REGNUM+ireg] = reg_position;
  372.           reg_position -= 4;
  373.           }
  374.       }
  375.  
  376.       /* hack: if argument regs are saved, guess these contain args */
  377.       if ((PROC_REG_MASK(proc_desc) & 0xF0) == 0) fci->num_args = -1;
  378.       else if ((PROC_REG_MASK(proc_desc) & 0x80) == 0) fci->num_args = 4;
  379.       else if ((PROC_REG_MASK(proc_desc) & 0x40) == 0) fci->num_args = 3;
  380.       else if ((PROC_REG_MASK(proc_desc) & 0x20) == 0) fci->num_args = 2;
  381.       else if ((PROC_REG_MASK(proc_desc) & 0x10) == 0) fci->num_args = 1;
  382.  
  383.       fci->saved_regs->regs[PC_REGNUM] = fci->saved_regs->regs[RA_REGNUM];
  384.     }
  385. }
  386.  
  387.  
  388. CORE_ADDR
  389. mips_push_arguments(nargs, args, sp, struct_return, struct_addr)
  390.   int nargs;
  391.   value *args;
  392.   CORE_ADDR sp;
  393.   int struct_return;
  394.   CORE_ADDR struct_addr;
  395. {
  396.   CORE_ADDR buf;
  397.   register i;
  398.   int accumulate_size = struct_return ? 4 : 0;
  399.   struct mips_arg { char *contents; int len; int offset; };
  400.   struct mips_arg *mips_args =
  401.       (struct mips_arg*)alloca(nargs * sizeof(struct mips_arg));
  402.   register struct mips_arg *m_arg;
  403.   for (i = 0, m_arg = mips_args; i < nargs; i++, m_arg++) {
  404.     extern value value_arg_coerce();
  405.     value arg = value_arg_coerce (args[i]);
  406.     m_arg->len = TYPE_LENGTH (VALUE_TYPE (arg));
  407.     /* This entire mips-specific routine is because doubles must be aligned
  408.      * on 8-byte boundaries. It still isn't quite right, because MIPS decided
  409.      * to align 'struct {int a, b}' on 4-byte boundaries (even though this
  410.      * breaks their varargs implementation...). A correct solution
  411.      * requires an simulation of gcc's 'alignof' (and use of 'alignof'
  412.      * in stdarg.h/varargs.h).
  413.      */
  414.     if (m_arg->len > 4) accumulate_size = (accumulate_size + 7) & -8;
  415.     m_arg->offset = accumulate_size;
  416.     accumulate_size = (accumulate_size + m_arg->len + 3) & -4;
  417.     m_arg->contents = VALUE_CONTENTS(arg);
  418.   }
  419.   accumulate_size = (accumulate_size + 7) & (-8);
  420.   if (accumulate_size < 16) accumulate_size = 16; 
  421.   sp -= accumulate_size;
  422.   for (i = nargs; m_arg--, --i >= 0; )
  423.     write_memory(sp + m_arg->offset, m_arg->contents, m_arg->len);
  424.   if (struct_return) {
  425.     buf = struct_addr;
  426.     write_memory(sp, &buf, sizeof(CORE_ADDR));
  427. }
  428.   return sp;
  429. }
  430.  
  431. /* MASK(i,j) == (1<<i) + (1<<(i+1)) + ... + (1<<j)). Assume i<=j<31. */
  432. #define MASK(i,j) ((1 << (j)+1)-1 ^ (1 << (i))-1)
  433.  
  434. void
  435. mips_push_dummy_frame()
  436. {
  437.   int ireg;
  438.   struct linked_proc_info *link = (struct linked_proc_info*)
  439.       xmalloc(sizeof(struct linked_proc_info));
  440.   mips_extra_func_info_t proc_desc = &link->info;
  441.   CORE_ADDR sp = read_register (SP_REGNUM);
  442.   CORE_ADDR save_address;
  443.   REGISTER_TYPE buffer;
  444.   link->next = linked_proc_desc_table;
  445.   linked_proc_desc_table = link;
  446. #define PUSH_FP_REGNUM 16 /* must be a register preserved across calls */
  447. #define GEN_REG_SAVE_MASK MASK(1,16)|MASK(24,28)|(1<<31)
  448. #define GEN_REG_SAVE_COUNT 22
  449. #define FLOAT_REG_SAVE_MASK MASK(0,19)
  450. #define FLOAT_REG_SAVE_COUNT 20
  451. #define SPECIAL_REG_SAVE_COUNT 4
  452.   /*
  453.    * The registers we must save are all those not preserved across
  454.    * procedure calls. Dest_Reg (see tm-mips.h) must also be saved.
  455.    * In addition, we must save the PC, and PUSH_FP_REGNUM.
  456.    * (Ideally, we should also save MDLO/-HI and FP Control/Status reg.)
  457.    *
  458.    * Dummy frame layout:
  459.    *  (high memory)
  460.    *     Saved PC
  461.    *    Saved MMHI, MMLO, FPC_CSR
  462.    *    Saved R31
  463.    *    Saved R28
  464.    *    ...
  465.    *    Saved R1
  466.    *    Saved D18 (i.e. F19, F18)
  467.    *    ...
  468.    *    Saved D0 (i.e. F1, F0)
  469.    *    CALL_DUMMY (subroutine stub; see m-mips.h)
  470.    *    Parameter build area (not yet implemented)
  471.    *  (low memory)
  472.    */
  473.   PROC_REG_MASK(proc_desc) = GEN_REG_SAVE_MASK;
  474.   PROC_FREG_MASK(proc_desc) = FLOAT_REG_SAVE_MASK;
  475.   PROC_REG_OFFSET(proc_desc) = /* offset of (Saved R31) from FP */
  476.       -sizeof(long) - 4 * SPECIAL_REG_SAVE_COUNT;
  477.   PROC_FREG_OFFSET(proc_desc) = /* offset of (Saved D18) from FP */
  478.       -sizeof(double) - 4 * (SPECIAL_REG_SAVE_COUNT + GEN_REG_SAVE_COUNT);
  479.   /* save general registers */
  480.   save_address = sp + PROC_REG_OFFSET(proc_desc);
  481.   for (ireg = 32; --ireg >= 0; )
  482.     if (PROC_REG_MASK(proc_desc) & (1 << ireg))
  483.       {
  484.     buffer = read_register (ireg);
  485.     write_memory (save_address, &buffer, sizeof(REGISTER_TYPE));
  486.     save_address -= 4;
  487.       }
  488.   /* save floating-points registers */
  489.   save_address = sp + PROC_FREG_OFFSET(proc_desc);
  490.   for (ireg = 32; --ireg >= 0; )
  491.     if (PROC_FREG_MASK(proc_desc) & (1 << ireg))
  492.       {
  493.     buffer = read_register (ireg + FP0_REGNUM);
  494.     write_memory (save_address, &buffer, 4);
  495.     save_address -= 4;
  496.       }
  497.   write_register (PUSH_FP_REGNUM, sp);
  498.   PROC_FRAME_REG(proc_desc) = PUSH_FP_REGNUM;
  499.   PROC_FRAME_OFFSET(proc_desc) = 0;
  500.   buffer = read_register (PC_REGNUM);
  501.   write_memory (sp - 4, &buffer, sizeof(REGISTER_TYPE));
  502.   buffer = read_register (HI_REGNUM);
  503.   write_memory (sp - 8, &buffer, sizeof(REGISTER_TYPE));
  504.   buffer = read_register (LO_REGNUM);
  505.   write_memory (sp - 12, &buffer, sizeof(REGISTER_TYPE));
  506.   buffer = read_register (FCRCS_REGNUM);
  507.   write_memory (sp - 16, &buffer, sizeof(REGISTER_TYPE));
  508.   sp -= 4 * (GEN_REG_SAVE_COUNT+FLOAT_REG_SAVE_COUNT+SPECIAL_REG_SAVE_COUNT);
  509.   write_register (SP_REGNUM, sp);
  510.   PROC_LOW_ADDR(proc_desc) = sp - CALL_DUMMY_SIZE + CALL_DUMMY_START_OFFSET;
  511.   PROC_HIGH_ADDR(proc_desc) = sp;
  512.   SET_PROC_DESC_IS_DUMMY(proc_desc);
  513.   PROC_PC_REG(proc_desc) = RA_REGNUM;
  514. }
  515.  
  516. void
  517. mips_pop_frame()
  518. { register int regnum;
  519.   FRAME frame = get_current_frame ();
  520.   CORE_ADDR new_sp = frame->frame;
  521.   mips_extra_func_info_t proc_desc = (mips_extra_func_info_t)frame->proc_desc;
  522.   if (PROC_DESC_IS_DUMMY(proc_desc))
  523.     {
  524.       struct linked_proc_info **ptr = &linked_proc_desc_table;;
  525.       for (; &ptr[0]->info != proc_desc; ptr = &ptr[0]->next )
  526.       if (ptr[0] == NULL) abort();
  527.       *ptr = ptr[0]->next;
  528.       free (ptr[0]);
  529.       write_register (HI_REGNUM, read_memory_integer(new_sp - 8, 4));
  530.       write_register (LO_REGNUM, read_memory_integer(new_sp - 12, 4));
  531.       write_register (FCRCS_REGNUM, read_memory_integer(new_sp - 16, 4));
  532.     }
  533.   write_register (PC_REGNUM, FRAME_SAVED_PC(frame));
  534.   if (frame->proc_desc) {
  535.     for (regnum = 32; --regnum >= 0; )
  536.       if (PROC_REG_MASK(proc_desc) & (1 << regnum))
  537.     write_register (regnum,
  538.           read_memory_integer (frame->saved_regs->regs[regnum], 4));
  539.     for (regnum = 32; --regnum >= 0; )
  540.       if (PROC_FREG_MASK(proc_desc) & (1 << regnum))
  541.     write_register (regnum + FP0_REGNUM,
  542.           read_memory_integer (frame->saved_regs->regs[regnum + FP0_REGNUM], 4));
  543.   }
  544.   write_register (SP_REGNUM, new_sp);
  545.   flush_cached_frames ();
  546.   set_current_frame (create_new_frame (new_sp, read_pc ()));
  547. }
  548.  
  549. static void
  550. mips_print_register(regnum, all)
  551.      int regnum, all;
  552. {
  553.       unsigned char raw_buffer[MAX_REGISTER_RAW_SIZE];
  554.       REGISTER_TYPE val;
  555.  
  556.       /* Get the data in raw format.  */
  557.       if (read_relative_register_raw_bytes (regnum, raw_buffer))
  558.     {
  559.       printf_filtered ("%s: [Invalid]", reg_names[regnum]);
  560.       return;
  561.     }
  562.       
  563.       /* If an even floating pointer register, also print as double. */
  564.       if (regnum >= FP0_REGNUM && regnum < FP0_REGNUM+32
  565.       && !((regnum-FP0_REGNUM) & 1)) {
  566.       read_relative_register_raw_bytes (regnum+1, raw_buffer+4);
  567.       printf_filtered ("(d%d: ", regnum-FP0_REGNUM);
  568.       val_print (builtin_type_double, raw_buffer, 0,
  569.              stdout, 0, 1, 0, Val_pretty_default);
  570.       printf_filtered ("); ");
  571.       }
  572.       fputs_filtered (reg_names[regnum], stdout);
  573. #ifndef NUMERIC_REG_NAMES
  574.       if (regnum < 32)
  575.       printf_filtered ("(r%d): ", regnum);
  576.       else
  577. #endif
  578.       printf_filtered (": ");
  579.  
  580.       /* If virtual format is floating, print it that way.  */
  581.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (regnum)) == TYPE_CODE_FLT
  582.       && ! INVALID_FLOAT (raw_buffer, REGISTER_VIRTUAL_SIZE(regnum))) {
  583.       val_print (REGISTER_VIRTUAL_TYPE (regnum), raw_buffer, 0,
  584.              stdout, 0, 1, 0, Val_pretty_default);
  585.       }
  586.       /* Else print as integer in hex.  */
  587.       else
  588.     {
  589.       long val;
  590.  
  591.       bcopy (raw_buffer, &val, sizeof (long));
  592.       SWAP_TARGET_AND_HOST ((char *)&val, sizeof (long));
  593.       if (val == 0)
  594.         printf_filtered ("0");
  595.       else if (all)
  596.         printf_filtered (local_hex_format(), val);
  597.       else
  598.         printf_filtered ("%s=%d", local_hex_string(val), val);
  599.     }
  600. }
  601.  
  602. /* Replacement for generic do_registers_info.  */
  603. void
  604. mips_do_registers_info (regnum, fpregs)
  605.      int regnum;
  606.      int fpregs;
  607. {
  608.   if (regnum != -1) {
  609.       mips_print_register (regnum, 0);
  610.       printf_filtered ("\n");
  611.   }
  612.   else {
  613.       for (regnum = 0; regnum < NUM_REGS; ) {
  614.       if ((!fpregs) && regnum >= FP0_REGNUM && regnum <= FCRIR_REGNUM) {
  615.         regnum++;
  616.         continue;
  617.       }
  618.       mips_print_register (regnum, 1);
  619.       regnum++;
  620.       if ((regnum & 3) == 0 || regnum == NUM_REGS)
  621.           printf_filtered (";\n");
  622.       else
  623.           printf_filtered ("; ");
  624.       }
  625.   }
  626. }
  627. /* Return number of args passed to a frame. described by FIP.
  628.    Can return -1, meaning no way to tell.  */
  629.  
  630. int
  631. mips_frame_num_args(fip)
  632.     FRAME fip;
  633. {
  634. #if 0
  635.     struct chain_info_t *p;
  636.  
  637.     p = mips_find_cached_frame(FRAME_FP(fip));
  638.     if (p->valid)
  639.         return p->the_info.numargs;
  640. #endif
  641.     return -1;
  642. }
  643.  
  644.  
  645. /* Bad floats: Returns 0 if P points to a valid IEEE floating point number,
  646.    1 if P points to a denormalized number or a NaN. LEN says whether this is
  647.    a single-precision or double-precision float */
  648. #define SINGLE_EXP_BITS  8
  649. #define DOUBLE_EXP_BITS 11
  650. int
  651. isa_NAN(p, len)
  652.      int *p, len;
  653. {
  654.   int exponent;
  655.   if (len == 4)
  656.     {
  657.       exponent = *p;
  658.       exponent = exponent << 1 >> (32 - SINGLE_EXP_BITS - 1);
  659.       return ((exponent == -1) || (! exponent && *p));
  660.     }
  661.   else if (len == 8)
  662.     {
  663.       exponent = *(p+1);
  664.       exponent = exponent << 1 >> (32 - DOUBLE_EXP_BITS - 1);
  665.       return ((exponent == -1) || (! exponent && *p * *(p+1)));
  666.     }
  667.   else return 1;
  668. }
  669.  
  670. /*
  671.  * Implemented for Irix 4.x by Garrett A. Wollman
  672.  */
  673. #ifdef USE_PROC_FS        /* Target-dependent /proc support */
  674.  
  675. #include <sys/time.h>
  676. #include <sys/procfs.h>
  677.  
  678. typedef unsigned int greg_t;    /* why isn't this defined? */
  679.  
  680. /*
  681.  * See the comment in m68k-tdep.c regarding the utility of these functions.
  682.  */
  683.  
  684. void 
  685. supply_gregset (gregsetp)
  686.      gregset_t *gregsetp;
  687. {
  688.   register int regno;
  689.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  690.  
  691.   /* FIXME: somewhere, there should be a #define for the meaning
  692.      of this magic number 32; we should use that. */
  693.   for(regno = 0; regno < 32; regno++)
  694.     supply_register (regno, (char *)(regp + regno));
  695.  
  696.   supply_register (PC_REGNUM, (char *)&(gregsetp->gp_pc));
  697.   supply_register (HI_REGNUM, (char *)&(gregsetp->gp_mdhi));
  698.   supply_register (LO_REGNUM, (char *)&(gregsetp->gp_mdlo));
  699.   supply_register (PS_REGNUM, (char *)&(gregsetp->gp_cause));
  700. }
  701.  
  702. void
  703. fill_gregset (gregsetp, regno)
  704.      gregset_t *gregsetp;
  705.      int regno;
  706. {
  707.   int regi;
  708.   register greg_t *regp = (greg_t *)(gregsetp->gp_regs);
  709.   extern char registers[];
  710.  
  711.   /* same FIXME as above wrt 32*/
  712.   for (regi = 0; regi < 32; regi++)
  713.     if ((regno == -1) || (regno == regi))
  714.       *(regp + regno) = *(greg_t *) ®isters[REGISTER_BYTE (regi)];
  715.  
  716.   if ((regno == -1) || (regno == PC_REGNUM))
  717.     gregsetp->gp_pc = *(greg_t *) ®isters[REGISTER_BYTE (PC_REGNUM)];
  718.  
  719.   if ((regno == -1) || (regno == PS_REGNUM))
  720.     gregsetp->gp_cause = *(greg_t *) ®isters[REGISTER_BYTE (PS_REGNUM)];
  721.  
  722.   if ((regno == -1) || (regno == HI_REGNUM))
  723.     gregsetp->gp_mdhi = *(greg_t *) ®isters[REGISTER_BYTE (HI_REGNUM)];
  724.  
  725.   if ((regno == -1) || (regno == LO_REGNUM))
  726.     gregsetp->gp_mdlo = *(greg_t *) ®isters[REGISTER_BYTE (LO_REGNUM)];
  727. }
  728.  
  729. /*
  730.  * Now we do the same thing for floating-point registers.
  731.  * We don't bother to condition on FP0_REGNUM since any
  732.  * reasonable MIPS configuration has an R3010 in it.
  733.  *
  734.  * Again, see the comments in m68k-tdep.c.
  735.  */
  736.  
  737. void
  738. supply_fpregset (fpregsetp)
  739.      fpregset_t *fpregsetp;
  740. {
  741.   register int regno;
  742.  
  743.   for (regno = 0; regno < 32; regno++)
  744.     supply_register (FP0_REGNUM + regno,
  745.              (char *)&fpregsetp->fp_r.fp_regs[regno]);
  746.  
  747.   supply_register (FCRCS_REGNUM, (char *)&fpregsetp->fp_csr);
  748.  
  749.   /* FIXME: how can we supply FCRIR_REGNUM?  SGI doesn't tell us. */
  750. }
  751.  
  752. void
  753. fill_fpregset (fpregsetp, regno)
  754.      fpregset_t *fpregsetp;
  755.      int regno;
  756. {
  757.   int regi;
  758.   char *from, *to;
  759.   extern char registers[];
  760.  
  761.   for (regi = FP0_REGNUM; regi < FP0_REGNUM + 32; regi++)
  762.     {
  763.       if ((regno == -1) || (regno == regi))
  764.     {
  765.       from = (char *) ®isters[REGISTER_BYTE (regi)];
  766.       to = (char *) &(fpregsetp->fp_r.fp_regs[regi]);
  767.       bcopy(from, to, REGISTER_RAW_SIZE (regno));
  768.     }
  769.     }
  770.  
  771.   if ((regno == -1) || (regno == FCRCS_REGNUM))
  772.     fpregsetp->fp_csr = *(unsigned *) ®isters[REGISTER_BYTE(FCRCS_REGNUM)];
  773. }
  774.  
  775. #endif /* USE_PROC_FS */
  776.  
  777. /* To skip prologues, I use this predicate. Returns either PC
  778.    itself if the code at PC does not look like a function prologue,
  779.    PC+4 if it does (our caller does not need anything more fancy). */
  780.  
  781. CORE_ADDR
  782. mips_skip_prologue(pc)
  783.      CORE_ADDR pc;
  784. {
  785.     struct symbol *f;
  786.     struct block *b;
  787.     unsigned long inst;
  788.     int offset;
  789.  
  790.     /* For -g modules and most functions anyways the
  791.        first instruction adjusts the stack.
  792.        But we allow some number of stores before the stack adjustment.
  793.        (These are emitted by varags functions compiled by gcc-2.0. */
  794.     for (offset = 0; offset < 100; offset += 4) {
  795.     inst = read_memory_integer(pc + offset, 4);
  796.     if ((inst & 0xffff0000) == 0x27bd0000) /* addiu $sp,$sp,offset */
  797.         return pc + offset + 4;
  798.     if ((inst & 0xFFE00000) != 0xAFA00000) /* sw reg,n($sp) */
  799.         break;
  800.     }
  801.  
  802.     /* Well, it looks like a frameless. Let's make sure.
  803.        Note that we are not called on the current PC,
  804.        but on the function`s start PC, and I have definitely
  805.        seen optimized code that adjusts the SP quite later */
  806.     b = block_for_pc(pc);
  807.     if (!b) return pc;
  808.  
  809.     f = lookup_symbol(".gdbinfo.", b, LABEL_NAMESPACE, 0, NULL);
  810.     if (!f) return pc;
  811.     /* Ideally, I would like to use the adjusted info
  812.        from mips_frame_info(), but for all practical
  813.        purposes it will not matter (and it would require
  814.        a different definition of SKIP_PROLOGUE())
  815.  
  816.        Actually, it would not hurt to skip the storing
  817.        of arguments on the stack as well. */
  818.     if (((mips_extra_func_info_t)SYMBOL_VALUE(f))->pdr.frameoffset)
  819.     return pc + 4;
  820.  
  821.     return pc;
  822. }
  823.  
  824. /* Figure out where the longjmp will land.
  825.    We expect the first arg to be a pointer to the jmp_buf structure from which
  826.    we extract the pc (JB_PC) that we will land at.  The pc is copied into PC.
  827.    This routine returns true on success. */
  828.  
  829. int
  830. get_longjmp_target(pc)
  831.      CORE_ADDR *pc;
  832. {
  833.   CORE_ADDR jb_addr;
  834.  
  835.   jb_addr = read_register(A0_REGNUM);
  836.  
  837.   if (target_read_memory(jb_addr + JB_PC * JB_ELEMENT_SIZE, pc,
  838.              sizeof(CORE_ADDR)))
  839.     return 0;
  840.  
  841.   SWAP_TARGET_AND_HOST(pc, sizeof(CORE_ADDR));
  842.  
  843.   return 1;
  844. }
  845. @
  846.